<?php

class Category extends Admin_Controller {

    public function __construct() {
        parent::__construct();

        $this->load->model('category_m');
    }

    /**
     * Default function
     */
    public function index() {
        //Get arrCategory
        $arrCategory = $this->category_m->get_all_by_parent_id(0);

        //Assign to view
        $this->assign('arrCategory', $arrCategory);
    }

    /**
     * Create method, creates a new category via ajax
     * @access public
     * @return void
     */
    public function create_ajax() {
        //Set response default
        $response = array(
            'error' => 1,
            'message' => 'Error Message'
        );

        if ($this->input->post()) {

            //Set array params
            $arrParams = array(
                'parent_id' => $this->input->post('cateParent'),
                'title' => $this->input->post('cateName'),
                'slug' => $this->removeUnicode($this->input->post('cateName')),
                'info' => $this->input->post('cateInfo'),
                'status' => $this->input->post('cateStatus'),
                'create_date' => time()
            );

            //Check title
            if ($this->category_m->check_title($arrParams['title'])) {
                $response['message'] = sprintf('Chuyên mục "%s" đã tồn tại.', $arrParams['title']);
                //Return response
                echo json_encode($response);
                exit;
            }

            //Insert categories
            $id = $this->category_m->insert($arrParams);

            //Check result
            if ($id > 0) {
                $response['error'] = 0;
                $response['message'] = 'Chuyên mục đã được thêm.';
                $response['id'] = $id;
            } else {
                $response['message'] = 'Có lỗi xảy ra.';
            }
        }

        //Return response
        echo json_encode($response);
        exit;
    }

    /**
     * Get all category 
     */
    public function get_all() {
        //Disable layout
        $this->disable_layout();

        //Set response default
        $response = array();

        $arrParams = NULL;
        if ($this->input->post()) {

            //Set array params
            $arrParams = array(
                'category_id' => $this->input->post('cateID'),
                'status' => $this->input->post('cateStatus')
            );
        }

        //Get data
        $response = $this->category_m->get_tree($arrParams);

        //Assign to view
        $this->assign('arrCategory', $response);
    }

    /**
     * update category by ajax
     * @access public
     * @return void
     */
    public function update_ajax() {
        //Set response default
        $response = array(
            'error' => 1,
            'message' => 'Error Message'
        );

        if ($this->input->post()) {

            $category_id = $this->input->post('cateID');

            //Check empty category
            if (empty($category_id)) {
                $response['message'] = 'Không tìm thấy chuyên mục này.';
                //Return response
                echo json_encode($response);
                exit;
            }

            //Set array params
            $arrParams = array(
                'parent_id' => $this->input->post('cateParent'),
                'title' => $this->input->post('cateName'),
                'slug' => $this->removeUnicode($this->input->post('cateName')),
                'info' => $this->input->post('cateInfo'),
                'status' => $this->input->post('cateStatus'),
                'create_date' => time()
            );

            //Insert categories
            $id = $this->category_m->update($category_id, $arrParams);

            //Check result
            if ($id > 0) {
                $response['error'] = 0;
                $response['message'] = 'Chuyên mục đã được thay đổi.';
                $response['id'] = $id;
            } else {
                $response['message'] = 'Có lỗi xảy ra.';
            }
        }

        //Return response
        echo json_encode($response);
        exit;
    }

    /**
     * delete category ajax
     */
    public function delete_ajax() {
        //Set response default
        $response = array(
            'error' => 1,
            'message' => 'Error Message'
        );

        if ($this->input->post()) {

            $category_id = $this->input->post('cateID');

            //Check empty category
            if (empty($category_id)) {
                $response['message'] = 'Không tìm thấy chuyên mục này.';
                //Return response
                echo json_encode($response);
                exit;
            }

            //Delete categories
            $result = $this->category_m->delete($category_id);

            //Check result
            if ($result) {
                $response['error'] = 0;
                $response['message'] = 'Chuyên mục đã được xóa.';
            } else {
                $response['message'] = 'Có lỗi xảy ra, vui lòng thử lại.';
            }
        }

        //Return response
        echo json_encode($response);
        exit;
    }

}